﻿using NLog;
using System;
using System.Diagnostics;

namespace Siasun.AFC.Common
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class LoggerInf
    {
        /// <summary>
        /// 私有nlog日志类
        /// </summary>
        private static Logger _log = LogManager.GetLogger("*");

        /// <summary>
        /// 获取上一层目录的类名称和方法名称
        /// </summary>
        /// <returns>The class method name.</returns>
        private static string GetClassMethodName(string msg)
        {
            string msgOut = string.Empty;
            string msgOut1 = string.Empty;
            string msgOut2 = string.Empty;
            StackTrace trace = new StackTrace();
            try
            {
                StackFrame frame = trace.GetFrame(2);
                string className = frame.GetMethod().ReflectedType.Name;
                string methodName = frame.GetMethod().Name;
                msgOut1 = className + "->>" + methodName;
            }
            catch (Exception)
            {
            }

            try
            {
                StackFrame frame = trace.GetFrame(1);
                string className = frame.GetMethod().ReflectedType.Name;
                string methodName = frame.GetMethod().Name;
                msgOut2 = className + "->" + methodName;
            }
            catch (Exception)
            {
            }
            if (string.IsNullOrEmpty(msgOut1))
            {
                msgOut = msgOut2 + ":" + msg;
            }
            else
            {
                msgOut = msgOut1 + "-" + msgOut2 + ":" + msg;
            }
            return msgOut;
        }

        /// <summary>
        /// Debug日志
        /// </summary>
        /// <returns>The debug.</returns>
        /// <param name="msg">日志数据</param>
        public static void Debug(string msg)
        {
            _log.Debug(GetClassMethodName(msg));
        }

        /// <summary>
        /// Info日志
        /// </summary>
        /// <returns>The info.</returns>
        /// <param name="msg">日志数据</param>
        public static void Info(string msg)
        {
            _log.Info(GetClassMethodName(msg));
        }

        /// <summary>
        /// Warn日志
        /// </summary>
        /// <returns>The warn.</returns>
        /// <param name="msg">日志数据</param>
        public static void Warn(string msg)
        {
            _log.Warn(GetClassMethodName(msg));
        }

        /// <summary>
        /// Error日志
        /// </summary>
        /// <returns>The error.</returns>
        /// <param name="msg">日志数据</param>
        public static void Error(string msg)
        {
            _log.Error(GetClassMethodName(msg));
        }

        /// <summary>
        /// Fatal日志
        /// </summary>
        /// <returns>The fatal.</returns>
        /// <param name="msg">日志数据</param>
        public static void Fatal(string msg)
        {
            _log.Fatal(GetClassMethodName(msg));
        }
    }
}